package com.systemframework.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.constants.Constants;
import com.systemframework.enums.BusinessType;
import com.systemframework.enums.ResultCodeEnum;
import com.systemframework.sys.dto.request.SysUserPsdResponseDTO;
import com.systemframework.sys.dto.request.SysUserRequestDTO;
import com.systemframework.sys.dto.response.SysUserResponseDTO;
import com.systemframework.sys.entity.SysUser;
import com.systemframework.sys.entity.SysUserRole;
import com.systemframework.sys.service.SysUserRoleService;
import com.systemframework.sys.service.SysUserService;
import com.systemframework.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-03-07 10:15
 */
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class SysUserController implements BaseCommonInterface<SysUserRequestDTO, SysUserResponseDTO> {

    private SysUserService sysUserService;

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @LicenseAnnotation
    @GetMapping(value = "/info")
    public Result<SysUserResponseDTO> info(){
        SysUserResponseDTO sysUserResponseVO = sysUserService.info();
        return Result.successResult(sysUserResponseVO);
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "用户信息")
    @Override
    @SaCheckPermission("sys:user:add")
    public Result<String> save( SysUserRequestDTO sysUserRequestDTO) {
        sysUserService.saveUser(sysUserRequestDTO);
        String msg=ObjectUtil.isEmpty(sysUserRequestDTO.getId())? Constants.USER_PASS_WORD: ResultCodeEnum.SUCCESS.msg();
        return Result.successResult(msg,null);
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "用户信息")
    @Override
    @SaCheckPermission("sys:user:edit")
    public Result<String> update(SysUserRequestDTO sysUserRequestDTO) {
        sysUserService.saveUser(sysUserRequestDTO);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "用户信息")
    @Override
    @SaCheckPermission("sys:user:delete")
    public Result<String> del(Integer id) {
        sysUserService.removeById(id);
        return Result.successResult();
    }

    @LicenseAnnotation
    @Override
    public Result<Page<SysUserResponseDTO>> lists(SysUserRequestDTO sysUserRequestDTO) {
        Page<SysUserResponseDTO> page=sysUserService.findLists(sysUserRequestDTO);
        return  Result.successResult(page);
    }



    @Override
    public Result<SysUserResponseDTO> findById(Integer id) {
        SysUserResponseDTO sysUserResponseDTO = sysUserService.findById(id);
        return Result.successResult(sysUserResponseDTO);
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "用户信息")
    @Override
    @SaCheckPermission("sys:user:delete")
    public Result<String> batchDel(List<SysUserResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(SysUserResponseDTO::getId).collect(Collectors.toList());
        if(!ids.isEmpty() && CollUtil.isNotEmpty(ids)){
            sysUserService.removeByIds(ids);
        }
        return Result.successResult();
    }





    @GetMapping(value = "/changeUser/{id}")
    @LicenseAnnotation
    public Result<String> changeUser(@PathVariable Integer id){
        sysUserService.changeUser(id);
        return Result.successResult();
    }

    @PostMapping(value = "/updatePsd")
    @LicenseAnnotation
    public Result<String> updatePsd(@Valid @RequestBody SysUserPsdResponseDTO sysUserPsdResponseDTO){
        sysUserService.updatePsd(sysUserPsdResponseDTO);
        return Result.successResult();
    }

}
